import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js'
import XInput from 'vux/src/components/x-input/index.vue'
import Group from 'vux/src/components/group/index.vue'
import Datetime from 'vux/src/components/datetime/index.vue'
import Picker from 'vux/src/components/picker/index.vue'
import Popup from 'vux/src/components/popup/index.vue'

var siteIda = global.siteId
var list = []
http.get(CONSTANT.GRADELIST + '?siteId=' + siteIda, {}, (res) => {
  // 获取年级段
  for (var i = 0; i < res.length; i++) {
    var listOne = {}
    if (res[i].resName != '幼儿') {
      listOne.name = res[i].resName
      listOne.value = res[i].resCode
      list.push(listOne)
    }
  }
})
export default {
  name: 'modifyInformation',
  components: {XInput, Group, Datetime, Picker, Popup},
  data () {
    return {
      minYear: 1900,
      maxYear: 2017,
      show: false,
      gradeList: [list],
      gradeListValue: [],
      siteId: '',
      studentId: '',
      idCardNeeds: true,
      name: '',
      mobile: '',
      numID: '',
      parentName: '',
      school: '',
      classNum: '',
      birthday: '',
      sex: '男',
      sfzyanzhen: function (value) {
        let r = /^(\d{18}|\d{17}x)$/;
        if (r.test(value)) {
          return {
            valid: true,
            msg: '身份证正确'
          }
        } else {
          return {
            valid: false,
            msg: '身份证错误'
          }
        }
      }
    }
  },
  mounted () {
    var _this = this
    _this.maxYear = new Date().getFullYear()
    _this.siteId = window.sessionStorage.getItem('siteId')
    _this.studentId = window.sessionStorage.getItem('studentId')
    console.log()
    http.get(CONSTANT.SETTING + '?siteId=' + _this.siteId + '&', {}, (res) => {
      // 获取身份证是否是必填项
      console.log(res)
      _this.idCardNeeds = res.idCardNeeds
      console.log(_this.idCardNeeds)
    })
    http.post(CONSTANT.STUDENTVIEW, {
      siteId: _this.siteId,
      studentId: _this.studentId
    }, (res) => {
      console.log(res)
      _this.name = res.name
      _this.mobile = res.cellPhone
      _this.numID = res.cardNo
      _this.gradeListValue[0] = res.grade
      _this.birthday = res.birthdayStr
      _this.parentName = res.parentName
      _this.school = res.school
      _this.sex = res.genderName
      if (_this.sex == '男') {
        this.$refs.man.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        this.$refs.woman.className = 'iconfont icon-danxuan1 iconfontNotChecked'
      } else {
        this.$refs.woman.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        this.$refs.man.className = 'iconfont icon-danxuan1 iconfontNotChecked'
      }
    })
  },
  methods: {
    overModify () {
      console.log(this)
      var _this = this
      var sexs = 0
      if (_this.sex == '男') {
        sexs = 0
      } else {
        sexs = 1
      }
      if (_this.name == '') {
        alert('请输入您的姓名！')
      } else if (_this.mobile == '') {
        alert('请输入您的手机号码！')
      } else if (_this.$refs.tel.valid == false) {
        alert('请输入正确的手机号码！')
      } else if (_this.numID == '' && _this.parentName == '') {
        alert('证件号码和外籍号码需填写一项')
      }
      /*else if (_this.numID == '' && _this.idCardNeeds == true) {
        alert('请输入您的身份证号码！')
      } else if (_this.$refs.idcard.valid == false) {
        alert('请输入正确的身份证号码！')
      } else if (_this.parentName == '') {
        alert('请输入家长姓名！')
      }*/ else if (_this.school == '') {
        alert('请输入学校！')
      } else if (_this.birthday == '') {
        alert('请选择出生日期！')
      } else if (!_this.gradeListValue[0]) {
        alert('请选择年级！')
      } else {
        http.post(CONSTANT.EDITPROFILE, {
          id: _this.studentId,
          siteId: _this.siteId,
          name: _this.name,
          cellPhone: _this.mobile,
          parentName: _this.parentName,
          school: _this.school,
          cardNo: _this.numID,
          grade: _this.gradeListValue[0],
          gender: sexs,
          birthday: _this.birthday
        }, (res) => {
          console.log(res)
          if (res.msg) {
            alert(res.msg)
          }
          window.history.go(-1)
        })
      }
    },
    chooseOK () {
      var _this = this
      _this.show = false
    },
    checkone (e) {
      // 男选择按钮
      var _this = this
      if (e.srcElement.className == 'iconfont icon-danxuan-xuanzhong iconfontChecked') {
        e.target.parentElement.children[1].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '男'
      } else {
        e.srcElement.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        e.target.parentElement.children[1].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '男'
      }
      console.log(this.sex)
    },
    checktwo (e) {
      // 女选择按钮
      var _this = this
      if (e.srcElement.className == 'iconfont icon-danxuan-xuanzhong iconfontChecked') {
        e.target.parentElement.children[0].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '女'
      } else {
        e.srcElement.className = 'iconfont icon-danxuan-xuanzhong iconfontChecked'
        e.target.parentElement.children[0].className = 'iconfont icon-danxuan1 iconfontNotChecked'
        _this.sex = '女'
      }
      console.log(this.sex)
    },
    comeBack () {
      window.history.go(-1)
    }
  }
}
